import { sjexp } from "./App";
import { things } from "./data";
import { deleteAllData,savePlayer } from "./SQL";
/**
 * 通过物品id查找物品
 * @param id 物品id
 * @returns 物品信息
 */
export function find_things_informations_by_id(id: number){
    return things[id];
}
/**
 * 通过物品id查找物品名称
 * @param id 物品id
 * @returns 物品名称
 */
export function get_thing_name_by_id(id: number):string{
    return find_things_informations_by_id(id).name;
}
/**
 * 将物品列表转为显示列表
 * @param bagList 物品列表
 * @returns 显示列表
 */
export function bag_show_list(bagList: { id: number; number: number }[]): string[] {
    const list: string[] = bagList.map(item => {
        const xx = find_things_informations_by_id(item.id);
        if (xx) {
            return xx.name + "*" + item.number;
        } else {
            console.warn(`未找到 id 为 ${item.id} 的物品信息`);
            return `未知物品*${item.number}`; // 或者返回 null/undefined，取决于你的需求
        }
    });
    return list;
}
/**
 * 打开玩家背包
 * @param entity 打开背包实体
 * @returns 
 */
export async function openBag(entity:GamePlayerEntity) {
    var option = ['打开背包','保存数据'];
    if (entity.player.name === '稳稳的小鹿9m2'){
        option.push('删档');
    }
    const xswq=find_things_informations_by_id(entity.zswq).name;//显示武器
    const xx = await entity.player.dialog<GameSelectDialogParams>({
        type: GameDialogType.SELECT,
        title: entity.player.name + ' 的玩家信息',
        content: `你好！${entity.player.name}
你的血量：${entity.hp}/${entity.maxHp}
你的等级：${entity.level} 
你的经验: ${entity.exp}/${sjexp(entity.level)} (还需要${sjexp(entity.level)-entity.exp}点经验升级)
你的金币: ${entity.coin}
你的灵石: ${entity.lingshi}
你的防御力: ${entity.fy}
你的主手武器：${xswq}
你的副手武器: ${find_things_informations_by_id(entity.fswq).name}
请问你要进行什么操作？`,
        titleBackgroundColor: new GameRGBAColor(0.93, 0.95, 0.54, 1.0),
        contentBackgroundColor: new GameRGBAColor(0.54, 0.68, 0.95, 1.0),
        options: option,
    });
    if (!xx) return;
    if (xx.index === 0){
        var show_list = bag_show_list(entity.bagList);
        if (show_list.length===0){
            await entity.player.dialog<GameSelectDialogParams>({
                type: GameDialogType.SELECT,
                title: entity.player.name + ' 的背包',
                content: `你的背包里没有物品!`,
                titleBackgroundColor: new GameRGBAColor(0.93, 0.95, 0.54, 1.0),
                contentBackgroundColor: new GameRGBAColor(0.54, 0.68, 0.95, 1.0),
                options: ['确定'],
            });
            return;
        }
        const bag = await entity.player.dialog<GameSelectDialogParams>({
            type: GameDialogType.SELECT,
            title: entity.player.name + ' 的背包',
            content: `${entity.player.name} 的背包物品`,
            titleBackgroundColor: new GameRGBAColor(0.93, 0.95, 0.54, 1.0),
            contentBackgroundColor: new GameRGBAColor(0.54, 0.68, 0.95, 1.0),
            options: show_list,
        });
        if (!bag) return;
        const athing = find_things_informations_by_id(entity.bagList[bag.index].id);
        const type = athing.type;
        if (type === 'zswq'){
            const is_use = await entity.player.dialog<GameSelectDialogParams>({
                type: GameDialogType.SELECT,
                title: athing.name,
                content: `武器名称: ${athing.name}
伤害: ${athing.damage}
介绍: ${athing.intro}
穿戴所需等级: ${athing.Mustlevel}
你要穿戴这个主手武器吗
`,
                titleBackgroundColor: new GameRGBAColor(0.93, 0.95, 0.54, 1.0),
                contentBackgroundColor: new GameRGBAColor(0.54, 0.68, 0.95, 1.0),
                options: ['穿戴','取消'],
            });
            if (!is_use) return;
            if (is_use.index===0 ){
                if (entity.level<athing.Mustlevel){
                    entity.player.directMessage('等级不足!');
                    return;
                }
                if (entity.zswq!=0)
                    entity.add({id:entity.zswq,number:1},false);
                entity.zswq=entity.bagList[bag.index].id;
                const handWears = entity.player.wearables(GameBodyPart.RIGHT_HAND); 
                // 假设只有1个装备 `handWears[0]` 
                entity.player.removeWearable(handWears[0]);
                entity.player.addWearable({
                    bodyPart: GameBodyPart.RIGHT_HAND,
                    mesh: find_things_informations_by_id(entity.zswq).mesh,
                    orientation: new GameQuaternion(0, 1, 0, 0).rotateY(Math.PI/2),
                    scale: new GameVector3(0.5, 0.5, 0.5),
                    offset: new GameVector3(0, 0, 0),
                });
                entity.delete({id:entity.bagList[bag.index].id,number:1})
                entity.player.directMessage('成功穿戴主手武器');
            }
        }else if (type === 'fswq'){
            const is_use = await entity.player.dialog<GameSelectDialogParams>({
                type: GameDialogType.SELECT,
                title: athing.name,
                content: `防具名称: ${athing.name}
伤害: ${athing.damage}
介绍: ${athing.intro}
穿戴所需等级: ${athing.Mustlevel}
你要穿戴这个防具吗
`,
                titleBackgroundColor: new GameRGBAColor(0.93, 0.95, 0.54, 1.0),
                contentBackgroundColor: new GameRGBAColor(0.54, 0.68, 0.95, 1.0),
                options: ['穿戴','取消'],
            });
            if (!is_use) return;
            if (is_use.index===0 ){
                if (entity.level<athing.Mustlevel){
                    entity.player.directMessage('等级不足!');
                    return;
                }
                if (entity.fswq!=0)
                    entity.add({id:entity.fswq,number:1},false);
                entity.fswq=entity.bagList[bag.index].id;
                entity.fy = (find_things_informations_by_id(entity.fswq).fy ); // 设置玩家防御力
                // 处理副手武器的穿戴
                const handWears = entity.player.wearables(GameBodyPart.LEFT_HAND); 
                // 假设只有1个装备 `handWears[0]` 
                entity.player.removeWearable(handWears[0]);
                entity.player.addWearable({
                    bodyPart: GameBodyPart.LEFT_HAND,
                    mesh: find_things_informations_by_id(entity.fswq).mesh,
                    orientation: new GameQuaternion(0, 1, 0, 0).rotateY(Math.PI/2),
                    scale: new GameVector3(1, 1, 1),
                    offset: new GameVector3(0, 0, 0),
                });
                entity.delete({id:entity.bagList[bag.index].id,number:1})
                entity.player.directMessage('成功穿戴副手武器');
            }
        }else {
            await entity.player.dialog<GameSelectDialogParams>({
                type: GameDialogType.SELECT,
                title: athing.name,
                content: '介绍:'+athing.intro,
                titleBackgroundColor: new GameRGBAColor(0.93, 0.95, 0.54, 1.0),
                contentBackgroundColor: new GameRGBAColor(0.54, 0.68, 0.95, 1.0),
                options: ['确定'],
            });
        }
        
    }else if (xx.value === '删档'){
        await deleteAllData();
        world.querySelectorAll('player').forEach((e)=>{
            e.player.kick();
        })
    }else if (xx.value === '保存数据'){
        await savePlayer(entity);
        entity.player.directMessage('数据保存成功!');
    }
}
